﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;


public partial class backstage_frend_link : System.Web.UI.Page
{
    // Fields

    protected FrendLink fl = new FrendLink();
    protected FrendLinkManager flm = new FrendLinkManager();


    // Methods
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
        try
        {
            this.fl.LinkName = this.txtLinkName.Value;
            this.fl.LinkUrl = this.txtLinkUrl.Value;
            this.fl.LanguageVersion = this.Session["language_version"].ToString();
            if (this.fileLinkLogo.PostedFile.FileName != "")
            {
                this.fl.LinkLogo = "/cn/frend_link_logo/" + DateTime.Now.ToString("yyyyMMddHHssmmffffff") + Path.GetExtension(this.fileLinkLogo.PostedFile.FileName);
            }
            this.fl.SortOrder = Convert.ToInt32(this.txtSortOrder.Value);
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                this.fl.Id = Convert.ToInt32(base.Request.QueryString["id"]);
                DataTable frendLinkById = this.flm.GetFrendLinkById(Convert.ToInt32(base.Request.QueryString["id"]));
                if (this.flm.ModifyFrendLink(this.fl))
                {
                    if (this.fileLinkLogo.PostedFile.FileName != "")
                    {
                        if (frendLinkById.Rows.Count > 0)
                        {
                            File.Delete(base.Server.MapPath(frendLinkById.Rows[0]["linkLogo"].ToString()));
                        }
                        this.fileLinkLogo.PostedFile.SaveAs(base.Server.MapPath(this.fl.LinkLogo));
                    }
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"frend_link_list.aspx\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
            }
            else if (this.flm.AddFrendLink(this.fl))
            {
                if (this.fileLinkLogo.PostedFile.FileName != "")
                {
                    this.fileLinkLogo.PostedFile.SaveAs(base.Server.MapPath(this.fl.LinkLogo));
                }
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"frend_link_list.aspx\";</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
            }
        }
        catch
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
            }
        }
    }

    private void FrendLinkBind()
    {
        try
        {
            DataTable frendLinkById = this.flm.GetFrendLinkById(Convert.ToInt32(base.Request.QueryString["id"]));
            if (frendLinkById.Rows.Count > 0)
            {
                this.txtLinkName.Value = frendLinkById.Rows[0]["linkName"].ToString();
                this.txtLinkUrl.Value = frendLinkById.Rows[0]["linkUrl"].ToString();
                if (frendLinkById.Rows[0]["linkLogo"].ToString() != "")
                {
                    this.imgLogo.Src = frendLinkById.Rows[0]["linkLogo"].ToString();
                    this.imgLogo.Style.Add("display", "block");
                }
                else
                {
                    this.imgLogo.Style.Add("display", "none");
                }
                this.txtSortOrder.Value = frendLinkById.Rows[0]["sortOrder"].ToString();
            }
        }
        catch
        {
        }
    }

    public StringBuilder GetTitle()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            if (base.Request.QueryString["ot"] == null)
            {
                return builder;
            }
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                builder.Append("编辑链接");
                return builder;
            }
            builder.Append("添加链接");
        }
        catch
        {
        }
        return builder;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        if (!base.IsPostBack && (base.Request.QueryString["ot"] != null))
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                this.FrendLinkBind();
            }
            else if (base.Request.QueryString["ot"] == "ad")
            {
                this.imgLogo.Style.Add("display", "none");
            }
        }
    }

}